package cn.zlj.pethome.product.service.impl;

import cn.zlj.pethome.basic.config.BussinessException;
import cn.zlj.pethome.basic.util.BaiduAuditUtils;
import cn.zlj.pethome.pet.domain.Pet;
import cn.zlj.pethome.pet.domain.PetOnlineAuditLog;
import cn.zlj.pethome.product.domain.Product;
import cn.zlj.pethome.product.domain.ProductDetail;
import cn.zlj.pethome.product.domain.ProductOnlineAuditLog;
import cn.zlj.pethome.product.mapper.ProductDetailMapper;
import cn.zlj.pethome.product.mapper.ProductMapper;
import cn.zlj.pethome.product.mapper.ProductOnlineAuditLogMapper;
import cn.zlj.pethome.product.service.IProductService;
import cn.zlj.pethome.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zlj
 * @since 2022-11-03
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;
    @Override
    public ProductDetail detail(Long id) {
        //System.out.println(id);
        return productMapper.detail(id);
    }

    @Override
    public void toffsale(List<Long> ids) {
        productMapper.toffsale(ids);
    }

    @Override
    public void onsale(List<Long> ids) {
        //非空处理
        if (ids == null) {
            throw new BussinessException("id不能为空");
        }
        //把所有下架的数据查出
        List<Product> all = productMapper.findByState(0);
        //使用s流过滤出含有ids的数据
        List<Product> collect = all.stream().filter(pet -> ids.contains(pet.getId())).collect(Collectors.toList());
        //百度ai审核数据
        collect.forEach(product -> {
            StringBuilder sb = new StringBuilder();
            if (!BaiduAuditUtils.TextCensor(product.getName())) {
                sb.append("服务上架百度AI审核名称失败，服务名称为：" + product.getName() + ";");
            }
//            if (product.getResources().split(",").length > 1) {
//                if (product.getResources().split(",")[0]!=null&&!BaiduAuditUtils.ImgCensor(product.getResources().split(",")[0])) {
//                    sb.append("服务上架百度AI审核图片失败，服务图片地址为：" + product.getResources() + ";");
//                }
//            } else {
//                if (product.getResources()!=null&&!BaiduAuditUtils.ImgCensor(product.getResources())) {
//                    sb.append("服务上架百度AI审核图片失败，服务图片地址为：" + product.getResources() + ";");
//                }
//            }
            Date date = new Date();
            //sb有数据就不上架加入错误日志提醒用户
            if (sb.length() > 0) {
                ProductOnlineAuditLog petOnlineAuditLog = new ProductOnlineAuditLog();
                petOnlineAuditLog.setNote(sb.toString());
                petOnlineAuditLog.setProductId(product.getId());
                petOnlineAuditLog.setAuditTime(new Date());
                petOnlineAuditLog.setState(0);
                productOnlineAuditLogMapper.add(petOnlineAuditLog);
            }
            else {
                product.setState(1);
                product.setOnsaletime(date);
                productMapper.update(product);
            }
        });
    }

    @Override
    public Product getPetDetail(Long id) {
        return productMapper.getPetDetail(id);
    }


    @Override
    public void update(Product product) {
        super.update(product);
        product.getDetail().setProductId(product.getId());
        productDetailMapper.update(product.getDetail());
    }

    @Override
    public void add(Product product) {
        product.setState(0);
        super.add(product);
        product.getDetail().setProductId(product.getId());
       productDetailMapper.add(product.getDetail());
    }
}
